package com.xysq.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.rockcent.action.CallbackListener;
import com.rockcent.model.VerificationModel;
import com.rockcent.model.constant.ModelType;
import com.umeng.analytics.MobclickAgent;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

import butterknife.InjectView;

public class QRCodeActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.btn_express)
    Button expressBtn;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.txt_rand_code)
    TextView randCodeTxt;
    @InjectView(R.id.img_qr_code)
    ImageView qrCodeImg;
    @InjectView(R.id.txt_customer_name)
    TextView nameTxt;
    @InjectView(R.id.txt_number)
    TextView numberTxt;
    @InjectView(R.id.layout_express)
    LinearLayout expressLayout;
    private int count;
    private int id;
    private String name;
    private int isDelivery;
    private int verificationId = 0;
    ModelType modelType;
    private static final int NOT_SUPPORT_DELIVERY = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        getExtra();
        initViewAndData();
    }

    private void loadData() {
        appAction.createQrCode(UserInfoKeeper.readPhoneNumber(QRCodeActivity.this), id, count, 0, new CallbackListener<VerificationModel>() {
            @Override
            public void onSuccess(VerificationModel data) {
                String qrCodeUrl = PropertiesUtil.getImageServer() + data.getQrCodeUrl();
                String randCode = data.getRandCode();
                verificationId = data.getId();
                randCodeTxt.setText(randCode);
                imageLoader.loadImage(qrCodeUrl, new ImageLoadingListener() {

                    @Override
                    public void onLoadingStarted(String s, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {
                        ToastUtil.showShort(QRCodeActivity.this, R.string.toast_load_qrcode_fail);
                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                        qrCodeImg.setImageBitmap(bitmap);
                        expressBtn.setEnabled(true);
                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {

                    }
                });
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(QRCodeActivity.this, message);
            }
        });
    }

    private void getExtra() {
        Intent intent = getIntent();
        count = intent.getIntExtra("count", 0);
        modelType = (ModelType) intent.getSerializableExtra("modelType");
        id = intent.getIntExtra("id", 0);
        name = intent.getStringExtra("name");
        isDelivery = intent.getIntExtra("isDelivery", 0);
    }

    private void initViewAndData() {
        expressBtn.setOnClickListener(this);
        backIbtn.setOnClickListener(this);
        nameTxt.setText(name);
        expressBtn.setEnabled(false);
        numberTxt.setText(getResources().getString(R.string.txt_exchange_num) + count);
        if (isDelivery == NOT_SUPPORT_DELIVERY | modelType != ModelType.CASH) {
            expressLayout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        if (view == expressBtn) {
            MobclickAgent.onEvent(this, "1022");
            if (verificationId != 0) {
                Intent intent = new Intent(QRCodeActivity.this, ConfirmExpressActivity.class);
                Bundle data = new Bundle();
                data.putInt("count", count);
                data.putString("name", name);
                data.putInt("verificationId", verificationId);
                intent.putExtras(data);
                startActivity(intent);
            }
        }
        if (view == backIbtn) {
            finish();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
        MobclickAgent.onPageStart("商品兑换页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("商品兑换页");
        MobclickAgent.onPause(this);
    }
}
